package com.scs.application.modules.fsd.controller;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.entity.FsdGroupUser;
import com.scs.application.modules.fsd.service.GroupUserService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description: 用户群组管理
 */
@RestController
@RequestMapping("/back_front/groups_users")
public class GroupUserController extends CrudController<GroupUserService, FsdGroupUser> {

    @GetMapping(value = "/getUsersByGroupService")
    public RestResult getUsersByGroupId(@RequestParam("id") String id) {
        return RestResult.ok("获取该用户所拥有的权限成功", service.getUsersByGroupId(id));
    }

    @PostMapping(value = "/changeUsersByGroupService")
    public RestResult saveGroupUser(@RequestBody Map<String, String> map) {
        if (service.saveGroupUser(StringUtils.stringArryExtract(map.get("idAry"), "userId"), map.get("id")))
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败！");
    }

    @GetMapping(value = "/getGroupsByUserService")
    public RestResult getGroupByUserId(@RequestParam("id") String id) {
        return RestResult.ok("获取该用户所属的用户群成功", service.getGroupByUserId(id));
    }

    @PostMapping(value = "/changeGroupsByUserService")
    public RestResult saveGroupsByUser(@RequestBody Map<String, String> map) {
        if (service.saveGroupsByUser(map.get("id"),StringUtils.stringArryExtract(map.get("idAry"), "groupId")))
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败！");
    }
}
